

; Callback Controller
; gets input to controller-input, filters out successive "pressed" messages and calls callbacks


(load "utility.scm") ; for vector-for-each


(define callback-controller%
  (class object%
    
    ; Private variables
    (define controller-keys      #f)
    (define controller-callbacks #f)
    (define controller-pressed   #f)
    
    ; Set Controller Keys
    (define/public (set-controller-keys keys)
      (set! controller-keys keys)
      (set! controller-pressed (make-vector (vector-length controller-keys) #f)))
    
    ; Set Controller Callbacks
    (define/public (set-controller-callbacks callbacks)
      (set! controller-callbacks callbacks))
    
    ; Get Controller Keys
    (define/public (get-controller-keys)
      controller-keys)
    
    ; Get Controller Callbacks
    (define/public (get-controller-callbacks)
      controller-callbacks)
    
    ; Controller Input - maps keys to callbacks
    (define/public (controller-input key pressed)
      (when controller-keys
        (vector-for-each (lambda (i e)
                           (when (and (eq? e key) (not (eq? pressed (vector-ref controller-pressed i))))
                             ((vector-ref controller-callbacks i) pressed)
                             (vector-set! controller-pressed i pressed)))
                         controller-keys)))
    
    ; Constructor/Initialization
    (super-new)))

